//
//  YWRecordOfAttendanceTableViewController.m
//  maintain
//
//  Created by Kyo on 16/9/29.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWRecordOfAttendanceTableViewController.h"
#import "UIColor+CustomColors.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "YWConst.h"
#import "YWMacro.h"
#import "YWCommonTool.h"
#import "NSDate+Additions.h"

//#import "YWImageView.h"
#import "YWPhotoView.h"
#import "YWAttendanceManager.h"
#import "YWAttendanceRecord.h"
#import "YWHandler.h"

#import "YWMeterReadingDetailRecordCell.h"
#import "YWEquipmentManager.h"
#import "YWBuildingManager.h"
#import "YWLocation.h"

@interface YWRecordOfAttendanceTableViewController ()
@property (nonatomic, strong) NSMutableArray *dateSource;
/** 图片容器 */
@property (nonatomic, strong) UIView* pictureView;
@end

@implementation YWRecordOfAttendanceTableViewController

- (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"recordsDetail", nil);//@"记录详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView registerClass:[YWMeterReadingDetailRecordCell class] forCellReuseIdentifier:YWMeterReadingDetailRecordCellIdentifier];
    
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (NSMutableArray*)dateSource
{
    if (!_dateSource) {
        _dateSource = [NSMutableArray array];
        [_dateSource addObject:[self executor]];
        [_dateSource addObject:[self locationText]];
        [_dateSource addObject:[self GPSLocation]];
        [_dateSource addObject:[self dateLabelString]];
        [_dateSource addObject:[self arriveNoteLabelString]];
    }
    return _dateSource;
}

- (NSString*)executor{
    NSString *handler = self.attendanceRecord.user.userName;
    NSString* type = self.attendanceRecord.type == YWAttendanceTypeArrive ? NSLocalizedString(@"arrive", nil) : NSLocalizedString(@"leave", nil);
    return [NSString stringWithFormat:@"%@人: %@",type,handler.length?handler:NSLocalizedString(@"none", nil)];
}

- (NSString*)locationText{
    NSString *location = [[YWBuildingManager shareManager] areaLocationWithLocationId:self.attendanceRecord.location.locationId];
    return [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"location", nil),location.length>0?location : NSLocalizedString(@"noDetailLocation", nil)];
}

- (NSString*)GPSLocation{
    NSString *gpsLocaiton = self.attendanceRecord.locationText.length>0?self.attendanceRecord.locationText : NSLocalizedString(@"none", nil);
    return [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"gpsLocation", nil),gpsLocaiton.length?gpsLocaiton:NSLocalizedString(@"none", nil)];
}

- (NSString*)dateLabelString
{
    NSString *timeString = [YWCommonTool showTimeWithNormalFormatter:self.attendanceRecord.arriveTime/1000];
    NSString* type = self.attendanceRecord.type == YWAttendanceTypeArrive ? NSLocalizedString(@"arrive", nil) : NSLocalizedString(@"leave", nil);
//    NSString *week = [@([NSDate dateWithTimeIntervalSince1970:self.attendanceRecord.arriveTime/1000].week) stringValue];
    NSString* arriveLabelString = [NSString stringWithFormat:@"%@%@：%@", type, timeString,NSLocalizedString(@"time", nil)];
    return arriveLabelString;
}

- (NSString*)arriveNoteLabelString
{
    NSString* arriveNoteLabelString = [NSString stringWithFormat:@"%@：%@", NSLocalizedString(@"note", nil),self.attendanceRecord.arriveRemark.length ?self.attendanceRecord.arriveRemark: NSLocalizedString(@"none", nil)];
    return arriveNoteLabelString;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dateSource.count;
}

//- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
//{
//    return [tableView fd_heightForCellWithIdentifier:YWMeterReadingDetailRecordCellIdentifier cacheByIndexPath:indexPath configuration:^(YWMeterReadingDetailRecordCell *cell) {
//        cell.contentLabel.text = self.dateSource[indexPath.row];
//    }];
//}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    YWMeterReadingDetailRecordCell * cell = [tableView dequeueReusableCellWithIdentifier:YWMeterReadingDetailRecordCellIdentifier];
    cell.contentLabel.text = self.dateSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
    [self.view endEditing:YES];
}

#pragma mark - tableview section foot view

- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return self.pictureView;
    }
    else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return self.pictureView.bounds.size.height;
    }
    else {
        return 0.01;
    }
}

- (UIView*)pictureView
{
    if (!_pictureView) {
        UIView* view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        _pictureView = view;
        CGFloat repairImageViewH = 0.1;
        NSArray* pictures = self.attendanceRecord.pictures;;
        
        if (pictures.count > 0) {
            YWPhotoView* imageV = [[YWPhotoView alloc] init];
            imageV.canAddPhoto = NO;
            imageV.photosArray = pictures;
            [imageV setFrame:CGRectMake(0, 10, imageV.frame.size.width, imageV.frame.size.height)];
            [_pictureView addSubview:imageV];
            repairImageViewH = imageV.frame.size.height + 20;
        }
        [_pictureView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, repairImageViewH)];
    }
    return _pictureView;
}

@end
